package com.hnkywl.sts.service.zjgl;

/**
 * Created by lw on 2015/11/10.
 */

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.htgl.CghtDao;
import com.hnkywl.sts.dao.htgl.HtmxDao;
import com.hnkywl.sts.dao.zjgl.XjyhDao;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.zjgl.Xjyh;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.zjgl
 * 类描述:   现金优惠服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class XjyhService {
    private XjyhDao xjyhDao;
    private CghtDao cghtDao;
    private HtmxDao htmxDao;

    /**
     * 查询所有现金优惠信息
     *
     * @param htbm
     * @param pageNo
     * @param rowsSize
     * @return
     */
    @Transactional
    public String findAllXjyh(String htbm, int pageNo, int rowsSize) {
        Page<Map> page = xjyhDao.findAllXjyh(htbm, pageNo, rowsSize);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    /**
     * 根据id查询一条数据的详细
     *
     * @param xjyhId
     * @return
     */
    @Transactional
    public Xjyh findXjyhDetail(Long xjyhId) {
        return xjyhDao.get(xjyhId);
    }

    /**
     * 添加一条记录
     *
     * @param xjyh
     */
    @Transactional
    public void addXjyh(Xjyh xjyh) {
        xjyh.setDateCreated(new Date());
        xjyh.setCreateUser(YgService.getCurrentYg());
        xjyh.setCreateOrgan(YgService.getCurrentYg().getZggs());
        xjyh.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        xjyh.setLastUpdated(new Date());
        xjyh.setSyzt(0);
        Cght xsht = this.cghtDao.get(xjyh.getCght().getId());
        xjyh.setKh(xsht.getLj().getXy().get(0));
        xjyh.setCzr(YgService.getCurrentYg());
        xjyh.setXsje(xjyh.getXsje());
        xjyh.setBz(xjyh.getBz());
        xjyh.setYhje(xjyh.getYhje());
        this.xjyhDao.save(xjyh);
    }

    /**
     * 修改
     *
     * @param xjyh
     */
    @Transactional
    public void updateXjyh(Xjyh xjyh, Long xjyhId) {
        Xjyh xjyh1 = xjyhDao.get(xjyhId);
        xjyh1.setLastUpdated(new Date());
        xjyh1.setBz(xjyh.getBz());
        xjyh1.setYhje(xjyh.getYhje());
        xjyh1.setCght(xjyh.getCght());
        this.xjyhDao.save(xjyh1);
    }

    /**
     * 删除
     *
     * @param xjyhId
     */
    @Transactional
    public void delXjyh(Long xjyhId) {
        Xjyh xjyh1 = xjyhDao.get(xjyhId);
        xjyh1.setDeleted(1);
        this.xjyhDao.save(xjyh1);
    }

    @Transactional
    public String findXsjeByHtid(Long xjyhId) {
        List<Map> allPage = this.xjyhDao.findXsjeByHtid(xjyhId);
        Gson gson = new Gson();
        String result = gson.toJson(allPage);
        return result;
    }

    @Autowired
    public void setCghtDao(CghtDao cghtDao) {
        this.cghtDao = cghtDao;
    }

    @Autowired
    public void setXjyhDao(XjyhDao xjyhDao) {
        this.xjyhDao = xjyhDao;
    }

    @Autowired
    public void setHtmxDao(HtmxDao htmxDao) {
        this.htmxDao = htmxDao;
    }

}
